package com.yksj.healthtalk.comm;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.yksj.healthtalk.ui.R;

/**
 * 
 * 用户引导页面
 *
 */
public class UserGuideFragment extends Fragment {
	
	ViewPager mViewPager;
	
	final int[] guideIds = new int[]{};
	private static final String GUI_TAG="10000";
	
	public static void showImage(FragmentManager  manager, int type){
		Fragment fragment = manager.findFragmentByTag(GUI_TAG);
		FragmentTransaction ft = manager.beginTransaction();
		if(fragment != null){
			ft.remove(fragment);
		}
		UserGuideFragment dialog = new UserGuideFragment();
		ft.add(dialog,GUI_TAG);
		ft.commitAllowingStateLoss();
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.user_guider_layout,null);
		mViewPager = (ViewPager)view.findViewById(R.id.pager);
		setAdapter();
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				if(arg0 == 0 && (mViewPager.getCurrentItem() == mViewPager.getAdapter().getCount()-1)){
					  FragmentManager fm = getFragmentManager();
					  FragmentTransaction beginTransaction = fm.beginTransaction();
					  beginTransaction.remove(UserGuideFragment.this);
					  beginTransaction.commitAllowingStateLoss();
					  if(getArguments() != null && getArguments().containsKey("type")){
						  InstructionsDialogFragment.show(10,getFragmentManager());
					  }
				}
			}
		});
		return view;
	}
	
	private void setAdapter(){
		List<Integer> list = new ArrayList<Integer>(5);
//		list.add(R.drawable.loadintroduce1);
//		list.add(R.drawable.loadintroduce2);
//		list.add(R.drawable.loadintroduce3);
//		list.add(R.drawable.loadintroduce4);
//		list.add(R.drawable.loadintroduce5);
		list.add(0);
		mViewPager.setAdapter(new ImageAdapter(list));
	}
	
	private class ImageAdapter extends PagerAdapter{
		private List<Integer>  views;
		
		public ImageAdapter(List<Integer> list){
			views = list;
		}
		
		@Override
		public int getCount() {
			return views.size();
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			ImageView view = new ImageView(getActivity());
			view.setScaleType(ScaleType.MATRIX);
			view.setBackgroundResource(views.get(position));
			container.addView(view);
			return view;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View)object);
		}
		
		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
	}
}
